package com.yby.mall.product.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yby.common.utils.R;
import com.yby.mall.product.dao.CategoryBrandRelationDao;
import com.yby.mall.product.vo.CategoryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.yby.mall.product.dao.CategoryDao;
import com.yby.mall.product.entity.CategoryEntity;
import com.yby.mall.product.service.CategoryService;
import org.springframework.transaction.annotation.Transactional;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Autowired
    private CategoryDao categoryDao;
    @Autowired
    private CategoryBrandRelationDao categoryBrandRelationDao;

    @Override
    public R getList() {
        List<CategoryEntity> list = categoryDao.selectList(null);
        //组装树形
        List<CategoryEntity> newList = list.stream().filter(categoryEntity -> categoryEntity.getParentCid().equals(Long.valueOf(0)))
                .map(categoryEntity -> {
                    categoryEntity.setChildren(getChild(categoryEntity, list));
                    return categoryEntity;
                }).sorted((o1, o2) -> {
                    return (o1.getSort() == null ? 0 : o1.getSort()) - (o2.getSort() == null ? 0 : o2.getSort());
                }).collect(Collectors.toList());
        return R.ok().put("data", newList);
    }

    @Override
    public R getList1() {
        List<CategoryEntity> list = categoryDao.selectList(null);
        List<Tree<String>> tree = tree(list);
        return R.ok().put("data", tree);
    }

    @Override
    public R save(CategoryVo categoryVo) {
        CategoryEntity categoryEntity = new CategoryEntity();
        BeanUtils.copyProperties(categoryVo, categoryEntity);
        categoryDao.insert(categoryEntity);
        return R.ok();
    }

    @Override
    @Transactional
    public R update(CategoryVo categoryVo) {
        CategoryEntity categoryEntity = new CategoryEntity();
        BeanUtils.copyProperties(categoryVo, categoryEntity);
        categoryDao.updateById(categoryEntity);
        //修改品牌分类表里面的分类名称字段
        categoryBrandRelationDao.updateByCatelogName(categoryVo.getCatId(),categoryVo.getName());
        return R.ok();
    }

    @Override
    public R updateSort(List<CategoryVo> list) {
        list.stream().forEach(categoryVo -> {
            CategoryEntity categoryEntity = new CategoryEntity();
            BeanUtils.copyProperties(categoryVo,categoryEntity);
            categoryDao.update(categoryEntity,Wrappers.<CategoryEntity>lambdaUpdate().eq(CategoryEntity::getCatId,categoryEntity.getCatId()));
        });
        return R.ok();
    }

    //递归查找所有菜单的子菜单
    public List<CategoryEntity> getChild(CategoryEntity root, List<CategoryEntity> all) {
        List<CategoryEntity> child = all.stream().filter(categoryEntity -> {
            return categoryEntity.getParentCid().equals(root.getCatId());
        }).map(categoryEntity -> {
            //找到子菜单
            categoryEntity.setChildren(getChild(categoryEntity, all));
            return categoryEntity;
        }).sorted((o1, o2) -> {
            //菜单排序
            return (o1.getSort() == null ? 0 : o1.getSort()) - (o2.getSort() == null ? 0 : o2.getSort());
        }).collect(Collectors.toList());
        return child;
    }


    public List<Tree<String>> tree(List<CategoryEntity> list) {
        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        // 自定义属性名 都要默认值的
        treeNodeConfig.setIdKey("id");
        treeNodeConfig.setChildrenKey("children");
        treeNodeConfig.setParentIdKey("parentId");
        treeNodeConfig.setWeightKey("sort");
        // 最大递归深度
        treeNodeConfig.setDeep(3);
        //转换器
        List<Tree<String>> treeNodes = TreeUtil.build(list, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getCatId().toString());
                    tree.setParentId(treeNode.getParentCid().toString());
                    tree.setName(treeNode.getName());
                    tree.setWeight(treeNode.getSort());
                    // 扩展属性 ...
                    tree.putExtra("catId", treeNode.getCatId());
                    tree.putExtra("name", treeNode.getName());
                    tree.putExtra("parentCid", treeNode.getParentCid());
                    tree.putExtra("catLevel", treeNode.getCatLevel());
                    tree.putExtra("productUnit", treeNode.getProductUnit());
                    tree.putExtra("showStatus", treeNode.getShowStatus());
                    tree.putExtra("sort", treeNode.getSort());
//                    tree.putExtra("catLevel", treeNode.getCatLevel());
//                    tree.putExtra("showStatus", treeNode.getShowStatus());
                });
        return treeNodes;
    }
}